/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.AA中等题.基本计算器II;

import com.sun.xml.internal.bind.v2.model.core.ID;

import java.util.LinkedList;

public class Main {

    public int calculate(String s) {
        LinkedList<Integer> num=new LinkedList<>();
        LinkedList<Integer> symbol=new LinkedList<>();
        StringBuilder stringBuilder=new StringBuilder();
        for (char c : s.toCharArray()) {
            switch (c){
                case ' ':
                    break;
                case '+':
                    symbol.addLast(1);
                    num.addLast(Integer.valueOf(stringBuilder.toString()));
                    stringBuilder=new StringBuilder();
                    break;
                case '-':
                    symbol.addLast(2);
                    num.addLast(Integer.valueOf(stringBuilder.toString()));
                    stringBuilder=new StringBuilder();
                    break;
                case '*':
                    symbol.addLast(3);
                    num.addLast(Integer.valueOf(stringBuilder.toString()));
                    stringBuilder=new StringBuilder();
                    break;
                case '/':
                    symbol.addLast(4);
                    num.addLast(Integer.valueOf(stringBuilder.toString()));
                    stringBuilder=new StringBuilder();
                    break;
                default:{
                    stringBuilder.append(c);
                }
            }
        }
        num.addLast(Integer.valueOf(stringBuilder.toString()));
        for (int i = symbol.size(); i >0 ; i--) {
            int symb=symbol.pollFirst();
            if (symb==3){
                num.addFirst(num.pollFirst()*num.pollFirst());
            }else {
                if (symb == 4) {
                    num.addFirst(num.pollFirst()/num.pollFirst());
                }
                else {
                    num.addLast(num.pollFirst());
                    symbol.addLast(symb);
                }
            }
        }
        num.addLast(num.pollFirst());
        for (Integer integer : symbol) {
            switch (integer){
                case 1:
                    num.addFirst(num.pollFirst()+num.pollFirst());
                    break;
                case 2:
                    num.addFirst(num.pollFirst()-num.pollFirst());
            }
        }
        return num.pollFirst();
    }


    public static void main(String arg[]) {//main--来自高金磊

        new Main().calculate("2*3-4");
    }
}
